Adds Joystick support for Android
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 05 Nov 2013 20:07:39 -0300
changeset 790724b4e98c6010
parent 7906 19f5760dc1a7
child 7908 b339af982b62
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.
README-android.txt
WhatsNew.txt
android-project/AndroidManifest.xml
android-project/default.properties
android-project/jni/Application.mk
android-project/project.properties
android-project/src/org/libsdl/app/SDLActivity.java
build-scripts/androidbuild.sh
include/SDL_hints.h
src/core/android/SDL_android.c
src/core/android/SDL_android.h
src/joystick/android/SDL_sysjoystick.c
src/joystick/android/SDL_sysjoystick.h
     1.1 --- a/README-android.txt	Sun Nov 03 11:34:03 2013 -0800
     1.2 +++ b/README-android.txt	Tue Nov 05 20:07:39 2013 -0300
     1.3 @@ -4,13 +4,14 @@
     1.4  
     1.5  Requirements:
     1.6  
     1.7 -Android SDK (version 10 or later)
     1.8 +Android SDK (version 12 or later)
     1.9  http://developer.android.com/sdk/index.html
    1.10  
    1.11  Android NDK r7 or later
    1.12  http://developer.android.com/sdk/ndk/index.html
    1.13  
    1.14  Minimum API level supported by SDL: 10 (Android 2.3.3)
    1.15 +Joystick support is available for API level >=12 devices.
    1.16  
    1.17  ================================================================================
    1.18   How the port works
    1.19 @@ -396,8 +397,11 @@
    1.20   Why is API level 10 the minimum required?
    1.21  ================================================================================
    1.22  
    1.23 -API level 10 is required because SDL requires some functionality for running not
    1.24 -available on older devices and some for building which is not in older NDK/SDKs.
    1.25 +API level 10 is the minimum required level at runtime (that is, on the device) 
    1.26 +because SDL requires some functionality for running not
    1.27 +available on older devices. Since the incorporation of joystick support into SDL,
    1.28 +the minimum SDK required to *build* SDL is version 12. Devices running API levels
    1.29 +10-11 are still supported, only with the joystick functionality disabled.
    1.30  
    1.31  Support for native OpenGL ES and ES2 applications was introduced in the NDK for
    1.32  API level 4 and 8. EGL was made a stable API in the NDK for API level 9, which
     2.1 --- a/WhatsNew.txt	Sun Nov 03 11:34:03 2013 -0800
     2.2 +++ b/WhatsNew.txt	Tue Nov 05 20:07:39 2013 -0300
     2.3 @@ -2,6 +2,16 @@
     2.4  This is a list of major changes in SDL's version history.
     2.5  
     2.6  ---------------------------------------------------------------------------
     2.7 +2.0.2:
     2.8 +---------------------------------------------------------------------------
     2.9 +
    2.10 +Android:
    2.11 +* Joystick support (minimum SDK version required to build SDL is now 12, 
    2.12 +  the required runtime version remains at 10, but on such devices joystick 
    2.13 +  support won't be available).
    2.14 +
    2.15 +
    2.16 +---------------------------------------------------------------------------
    2.17  2.0.1:
    2.18  ---------------------------------------------------------------------------
    2.19  
     3.1 --- a/android-project/AndroidManifest.xml	Sun Nov 03 11:34:03 2013 -0800
     3.2 +++ b/android-project/AndroidManifest.xml	Tue Nov 05 20:07:39 2013 -0300
     3.3 @@ -31,7 +31,7 @@
     3.4      </application>
     3.5  
     3.6      <!-- Android 2.3.3 -->
     3.7 -    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" />
     3.8 +    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="12" />
     3.9  
    3.10      <!-- OpenGL ES 2.0 -->
    3.11      <uses-feature android:glEsVersion="0x00020000" /> 
     4.1 --- a/android-project/default.properties	Sun Nov 03 11:34:03 2013 -0800
     4.2 +++ b/android-project/default.properties	Tue Nov 05 20:07:39 2013 -0300
     4.3 @@ -8,4 +8,4 @@
     4.4  # project structure.
     4.5  
     4.6  # Project target.
     4.7 -target=android-7
     4.8 +target=android-12
     5.1 --- a/android-project/jni/Application.mk	Sun Nov 03 11:34:03 2013 -0800
     5.2 +++ b/android-project/jni/Application.mk	Tue Nov 05 20:07:39 2013 -0300
     5.3 @@ -2,3 +2,5 @@
     5.4  # Uncomment this if you're using STL in your project
     5.5  # See CPLUSPLUS-SUPPORT.html in the NDK documentation for more information
     5.6  # APP_STL := stlport_static 
     5.7 +
     5.8 +APP_ABI := armeabi armeabi-v7a x86
     6.1 --- a/android-project/project.properties	Sun Nov 03 11:34:03 2013 -0800
     6.2 +++ b/android-project/project.properties	Tue Nov 05 20:07:39 2013 -0300
     6.3 @@ -11,4 +11,4 @@
     6.4  #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
     6.5  
     6.6  # Project target.
     6.7 -target=android-10
     6.8 +target=android-12
     7.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Sun Nov 03 11:34:03 2013 -0800
     7.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Tue Nov 05 20:07:39 2013 -0300
     7.3 @@ -16,6 +16,10 @@
     7.4  import android.media.*;
     7.5  import android.hardware.*;
     7.6  
     7.7 +import java.lang.*;
     7.8 +import java.util.List;
     7.9 +import java.util.ArrayList;
    7.10 +
    7.11  
    7.12  /**
    7.13      SDL Activity
    7.14 @@ -31,10 +35,11 @@
    7.15      protected static SDLSurface mSurface;
    7.16      protected static View mTextEdit;
    7.17      protected static ViewGroup mLayout;
    7.18 +    protected static SDLJoystickHandler mJoystickHandler;
    7.19  
    7.20      // This is what SDL runs in. It invokes SDL_main(), eventually
    7.21      protected static Thread mSDLThread;
    7.22 -
    7.23 +    
    7.24      // Audio
    7.25      protected static Thread mAudioThread;
    7.26      protected static AudioTrack mAudioTrack;
    7.27 @@ -60,6 +65,13 @@
    7.28  
    7.29          // Set up the surface
    7.30          mSurface = new SDLSurface(getApplication());
    7.31 +        
    7.32 +        if(Build.VERSION.SDK_INT >= 12) {
    7.33 +            mJoystickHandler = new SDLJoystickHandler_API12();
    7.34 +        }
    7.35 +        else {
    7.36 +            mJoystickHandler = new SDLJoystickHandler();
    7.37 +        }
    7.38  
    7.39          mLayout = new AbsoluteLayout(this);
    7.40          mLayout.addView(mSurface);
    7.41 @@ -236,6 +248,10 @@
    7.42      public static native void nativePause();
    7.43      public static native void nativeResume();
    7.44      public static native void onNativeResize(int x, int y, int format);
    7.45 +    public static native void onNativePadDown(int padId, int keycode);
    7.46 +    public static native void onNativePadUp(int padId, int keycode);
    7.47 +    public static native void onNativeJoy(int joyId, int axis,
    7.48 +                                          float value);
    7.49      public static native void onNativeKeyDown(int keycode);
    7.50      public static native void onNativeKeyUp(int keycode);
    7.51      public static native void onNativeKeyboardFocusLost();
    7.52 @@ -406,6 +422,23 @@
    7.53          }
    7.54          return Arrays.copyOf(filtered, used);
    7.55      }
    7.56 +            
    7.57 +    // Joystick glue code, just a series of stubs that redirect to the SDLJoystickHandler instance
    7.58 +    public static int getNumJoysticks() {
    7.59 +        return mJoystickHandler.getNumJoysticks();
    7.60 +    }
    7.61 +    
    7.62 +    public static String getJoystickName(int joy) {
    7.63 +        return mJoystickHandler.getJoystickName(joy);
    7.64 +    }
    7.65 +    
    7.66 +    public static int getJoystickAxes(int joy) {
    7.67 +        return mJoystickHandler.getJoystickAxes(joy);
    7.68 +    }
    7.69 +    
    7.70 +    public static int getJoyId(int devId) {
    7.71 +        return mJoystickHandler.getJoyId(devId);
    7.72 +    }
    7.73  }
    7.74  
    7.75  /**
    7.76 @@ -451,6 +484,10 @@
    7.77  
    7.78          mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    7.79          mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
    7.80 +        
    7.81 +        if(Build.VERSION.SDK_INT >= 12) {
    7.82 +            setOnGenericMotionListener(new SDLGenericMotionHandler_API12());
    7.83 +        }
    7.84  
    7.85          // Some arbitrary defaults to avoid a potential division by zero
    7.86          mWidth = 1.0f;
    7.87 @@ -557,16 +594,26 @@
    7.88      // Key events
    7.89      @Override
    7.90      public boolean onKey(View  v, int keyCode, KeyEvent event) {
    7.91 -        
    7.92 -        if (event.getAction() == KeyEvent.ACTION_DOWN) {
    7.93 -            //Log.v("SDL", "key down: " + keyCode);
    7.94 -            SDLActivity.onNativeKeyDown(keyCode);
    7.95 -            return true;
    7.96 -        }
    7.97 -        else if (event.getAction() == KeyEvent.ACTION_UP) {
    7.98 -            //Log.v("SDL", "key up: " + keyCode);
    7.99 -            SDLActivity.onNativeKeyUp(keyCode);
   7.100 -            return true;
   7.101 +        // Dispatch the different events depending on where they come from
   7.102 +        if(event.getSource() == InputDevice.SOURCE_KEYBOARD) {
   7.103 +            if (event.getAction() == KeyEvent.ACTION_DOWN) {
   7.104 +                //Log.v("SDL", "key down: " + keyCode);
   7.105 +                SDLActivity.onNativeKeyDown(keyCode);
   7.106 +                return true;
   7.107 +            }
   7.108 +            else if (event.getAction() == KeyEvent.ACTION_UP) {
   7.109 +                //Log.v("SDL", "key up: " + keyCode);
   7.110 +                SDLActivity.onNativeKeyUp(keyCode);
   7.111 +                return true;
   7.112 +            }
   7.113 +        } else if ( (event.getSource() & 0x00000401) != 0 || /* API 12: SOURCE_GAMEPAD */
   7.114 +                   (event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
   7.115 +            int id = SDLActivity.getJoyId( event.getDeviceId() );
   7.116 +            if (event.getAction() == KeyEvent.ACTION_DOWN) {
   7.117 +                SDLActivity.onNativePadDown(id, keyCode);
   7.118 +            } else if (event.getAction() == KeyEvent.ACTION_UP) {
   7.119 +                SDLActivity.onNativePadUp(id, keyCode);
   7.120 +            }
   7.121          }
   7.122          
   7.123          return false;
   7.124 @@ -646,8 +693,7 @@
   7.125                                        y / SensorManager.GRAVITY_EARTH,
   7.126                                        event.values[2] / SensorManager.GRAVITY_EARTH - 1);
   7.127          }
   7.128 -    }
   7.129 -    
   7.130 +    }    
   7.131  }
   7.132  
   7.133  /* This is a fake invisible editor view that receives the input and defines the
   7.134 @@ -769,3 +815,95 @@
   7.135  
   7.136  }
   7.137  
   7.138 +/* A null joystick handler for API level < 12 devices (the accelerometer is handled separately) */
   7.139 +class SDLJoystickHandler {
   7.140 +    public int getNumJoysticks() {
   7.141 +        return 0;
   7.142 +    }
   7.143 +    
   7.144 +    public String getJoystickName(int joy) {
   7.145 +        return "";
   7.146 +    }
   7.147 +    
   7.148 +    public int getJoystickAxes(int joy) {
   7.149 +        return 0;
   7.150 +    }
   7.151 +    
   7.152 +    public int getJoyId(int devId) {
   7.153 +        return 0;
   7.154 +    }
   7.155 +}
   7.156 +
   7.157 +/* Actual joystick functionality available for API >= 12 devices */
   7.158 +class SDLJoystickHandler_API12 extends SDLJoystickHandler {
   7.159 +    private List<Integer> mJoyIdList;
   7.160 +    
   7.161 +    // Create a list of valid ID's the first time this function is called
   7.162 +    private void createJoystickList() {
   7.163 +        if(mJoyIdList != null) {
   7.164 +            return;
   7.165 +        }
   7.166 +        
   7.167 +        mJoyIdList = new ArrayList<Integer>();
   7.168 +        int[] deviceIds = InputDevice.getDeviceIds();
   7.169 +        for(int i=0; i<deviceIds.length; i++) {
   7.170 +            if( (InputDevice.getDevice(deviceIds[i]).getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
   7.171 +                mJoyIdList.add(deviceIds[i]);
   7.172 +            }
   7.173 +        }
   7.174 +    }
   7.175 +    
   7.176 +    public int getNumJoysticks() {
   7.177 +        createJoystickList();
   7.178 +        
   7.179 +        return mJoyIdList.size();
   7.180 +    }
   7.181 +    
   7.182 +    public String getJoystickName(int joy) {
   7.183 +        createJoystickList();
   7.184 +        return InputDevice.getDevice(mJoyIdList.get(joy)).getName();
   7.185 +    }
   7.186 +    
   7.187 +    public int getJoystickAxes(int joy) {
   7.188 +        createJoystickList();
   7.189 +        return InputDevice.getDevice(mJoyIdList.get(joy)).getMotionRanges().size();
   7.190 +    }
   7.191 +    
   7.192 +    public int getJoyId(int devId) {
   7.193 +        int i=0;
   7.194 +        
   7.195 +        createJoystickList();
   7.196 +        
   7.197 +        for(i=0; i<mJoyIdList.size(); i++) {
   7.198 +            if(mJoyIdList.get(i) == devId) {
   7.199 +                return i;
   7.200 +            }
   7.201 +        }
   7.202 +        
   7.203 +        return -1;
   7.204 +    }
   7.205 +    
   7.206 +}
   7.207 +
   7.208 +class SDLGenericMotionHandler_API12 extends Activity implements View.OnGenericMotionListener {
   7.209 +    // Generic Motion (mouse hover, joystick...) events go here
   7.210 +    // We only have joysticks yet
   7.211 +    @Override
   7.212 +    public boolean onGenericMotion(View v, MotionEvent event) {
   7.213 +        int actionPointerIndex = event.getActionIndex();
   7.214 +        int action = event.getActionMasked();
   7.215 +        
   7.216 +        if ( (event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) {
   7.217 +            switch(action) {
   7.218 +                case MotionEvent.ACTION_MOVE:
   7.219 +                    int id = SDLActivity.getJoyId( event.getDeviceId() );
   7.220 +                    float x = event.getAxisValue(MotionEvent.AXIS_X, actionPointerIndex);
   7.221 +                    float y = event.getAxisValue(MotionEvent.AXIS_Y, actionPointerIndex);
   7.222 +                    SDLActivity.onNativeJoy(id, 0, x);
   7.223 +                    SDLActivity.onNativeJoy(id, 1, y);
   7.224 +                    break;
   7.225 +            }
   7.226 +        }
   7.227 +        return true;
   7.228 +    }
   7.229 +}
   7.230 \ No newline at end of file
     8.1 --- a/build-scripts/androidbuild.sh	Sun Nov 03 11:34:03 2013 -0800
     8.2 +++ b/build-scripts/androidbuild.sh	Tue Nov 05 20:07:39 2013 -0300
     8.3 @@ -24,6 +24,7 @@
     8.4  if [ -z "$1" ] || [ -z "$SOURCES" ]; then
     8.5      echo "Usage: androidbuild.sh com.yourcompany.yourapp < sources.list"
     8.6      echo "Usage: androidbuild.sh com.yourcompany.yourapp source1.c source2.c ...sourceN.c"
     8.7 +    echo "To copy SDL source instead of symlinking: COPYSOURCE=1 androidbuild.sh ... "
     8.8      exit 1
     8.9  fi
    8.10  
    8.11 @@ -63,9 +64,15 @@
    8.12  
    8.13  # Copy SDL sources
    8.14  mkdir -p $BUILDPATH/jni/SDL
    8.15 -cp -r $SDLPATH/src $BUILDPATH/jni/SDL
    8.16 -cp -r $SDLPATH/include $BUILDPATH/jni/SDL
    8.17 -cp $SDLPATH/Android.mk $BUILDPATH/jni/SDL
    8.18 +if [ -z "$COPYSOURCE" ]; then
    8.19 +    ln -s $SDLPATH/src $BUILDPATH/jni/SDL
    8.20 +    ln -s $SDLPATH/include $BUILDPATH/jni/SDL
    8.21 +else
    8.22 +    cp -r $SDLPATH/src $BUILDPATH/jni/SDL
    8.23 +    cp -r $SDLPATH/include $BUILDPATH/jni/SDL
    8.24 +fi
    8.25 +
    8.26 +cp -r $SDLPATH/Android.mk $BUILDPATH/jni/SDL
    8.27  sed -i "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/jni/src/Android.mk
    8.28  sed -i "s|org\.libsdl\.app|$APP|g" $BUILDPATH/AndroidManifest.xml
    8.29  
     9.1 --- a/include/SDL_hints.h	Sun Nov 03 11:34:03 2013 -0800
     9.2 +++ b/include/SDL_hints.h	Tue Nov 05 20:07:39 2013 -0300
     9.3 @@ -203,6 +203,16 @@
     9.4   *    "LandscapeLeft", "LandscapeRight", "Portrait" "PortraitUpsideDown"
     9.5   */
     9.6  #define SDL_HINT_ORIENTATIONS "SDL_IOS_ORIENTATIONS"
     9.7 +    
     9.8 +/**
     9.9 + *  \brief  A variable controlling whether an Android built-in accelerometer should be
    9.10 + *  listed as a joystick device, rather than listing actual joysticks only.
    9.11 + *
    9.12 + *  This variable can be set to the following values:
    9.13 + *    "0"       - List only real joysticks and accept input from them
    9.14 + *    "1"       - List real joysticks along with the accelerometer as if it were a 3 axis joystick (the default).
    9.15 + */
    9.16 +#define SDL_HINT_ACCEL_AS_JOY "SDL_ACCEL_AS_JOY"
    9.17  
    9.18  
    9.19  /**
    10.1 --- a/src/core/android/SDL_android.c	Sun Nov 03 11:34:03 2013 -0800
    10.2 +++ b/src/core/android/SDL_android.c	Tue Nov 05 20:07:39 2013 -0300
    10.3 @@ -146,6 +146,30 @@
    10.4      Android_SetScreenResolution(width, height, format);
    10.5  }
    10.6  
    10.7 +// Paddown
    10.8 +void Java_org_libsdl_app_SDLActivity_onNativePadDown(
    10.9 +                                    JNIEnv* env, jclass jcls,
   10.10 +                                    jint padId, jint keycode)
   10.11 +{
   10.12 +    Android_OnPadDown(padId, keycode);
   10.13 +}
   10.14 +
   10.15 +// Padup
   10.16 +void Java_org_libsdl_app_SDLActivity_onNativePadUp(
   10.17 +                                   JNIEnv* env, jclass jcls,
   10.18 +                                   jint padId, jint keycode)
   10.19 +{
   10.20 +    Android_OnPadUp(padId, keycode);
   10.21 +}
   10.22 +
   10.23 +// Padup
   10.24 +void Java_org_libsdl_app_SDLActivity_onNativeJoy(
   10.25 +                                    JNIEnv* env, jclass jcls,
   10.26 +                                    jint joyId, jint axis, jfloat value)
   10.27 +{
   10.28 +    Android_OnJoy(joyId, axis, value);
   10.29 +}
   10.30 +
   10.31  
   10.32  /* Surface Created */
   10.33  void Java_org_libsdl_app_SDLActivity_onNativeSurfaceChanged(JNIEnv* env, jclass jcls)
   10.34 @@ -1212,6 +1236,62 @@
   10.35      return number;
   10.36  }
   10.37  
   10.38 +/* return the total number of plugged in joysticks */
   10.39 +int Android_JNI_GetNumJoysticks()
   10.40 +{
   10.41 +    JNIEnv* env = Android_JNI_GetEnv();
   10.42 +    if (!env) {
   10.43 +        return -1;
   10.44 +    }
   10.45 +    
   10.46 +    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getNumJoysticks", "()I");
   10.47 +    if (!mid) {
   10.48 +        return -1;
   10.49 +    }
   10.50 +    
   10.51 +    return (int)(*env)->CallStaticIntMethod(env, mActivityClass, mid);
   10.52 +}
   10.53 +
   10.54 +/* Return the name of joystick number "i" */
   10.55 +char* Android_JNI_GetJoystickName(int i)
   10.56 +{
   10.57 +    JNIEnv* env = Android_JNI_GetEnv();
   10.58 +    if (!env) {
   10.59 +        return SDL_strdup("");
   10.60 +    }
   10.61 +    
   10.62 +    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getJoystickName", "(I)Ljava/lang/String;");
   10.63 +    if (!mid) {
   10.64 +        return SDL_strdup("");
   10.65 +    }
   10.66 +    jstring string = (jstring)((*env)->CallStaticObjectMethod(env, mActivityClass, mid, i));
   10.67 +    const char* utf = (*env)->GetStringUTFChars(env, string, 0);
   10.68 +    if (!utf) {
   10.69 +        return SDL_strdup("");
   10.70 +    }
   10.71 +    
   10.72 +    char* text = SDL_strdup(utf);
   10.73 +    (*env)->ReleaseStringUTFChars(env, string, utf);
   10.74 +    return text;
   10.75 +}
   10.76 +
   10.77 +/* return the number of axes in the given joystick */
   10.78 +int Android_JNI_GetJoystickAxes(int joy)
   10.79 +{
   10.80 +    JNIEnv* env = Android_JNI_GetEnv();
   10.81 +    if (!env) {
   10.82 +        return -1;
   10.83 +    }
   10.84 +    
   10.85 +    jmethodID mid = (*env)->GetStaticMethodID(env, mActivityClass, "getJoystickAxes", "(I)I");
   10.86 +    if (!mid) {
   10.87 +        return -1;
   10.88 +    }
   10.89 +    
   10.90 +    return (int)(*env)->CallIntMethod(env, mActivityClass, mid, joy);
   10.91 +}
   10.92 +
   10.93 +
   10.94  /* sends message to be handled on the UI event dispatch thread */
   10.95  int Android_JNI_SendMessage(int command, int param)
   10.96  {
    11.1 --- a/src/core/android/SDL_android.h	Sun Nov 03 11:34:03 2013 -0800
    11.2 +++ b/src/core/android/SDL_android.h	Tue Nov 05 20:07:39 2013 -0300
    11.3 @@ -64,6 +64,11 @@
    11.4  
    11.5  /* Power support */
    11.6  int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent);
    11.7 +    
    11.8 +/* Joystick support */
    11.9 +int Android_JNI_GetNumJoysticks();
   11.10 +char* Android_JNI_GetJoystickName(int i);
   11.11 +int Android_JNI_GetJoystickAxes(int joy);
   11.12  
   11.13  /* Touch support */
   11.14  int Android_JNI_GetTouchDeviceIds(int **ids);
    12.1 --- a/src/joystick/android/SDL_sysjoystick.c	Sun Nov 03 11:34:03 2013 -0800
    12.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Tue Nov 05 20:07:39 2013 -0300
    12.3 @@ -29,11 +29,48 @@
    12.4  #include "SDL_error.h"
    12.5  #include "SDL_events.h"
    12.6  #include "SDL_joystick.h"
    12.7 +#include "SDL_hints.h"
    12.8  #include "../SDL_sysjoystick.h"
    12.9  #include "../SDL_joystick_c.h"
   12.10  #include "../../core/android/SDL_android.h"
   12.11  
   12.12 -static const char *accelerometerName = "Android accelerometer";
   12.13 +#define ANDROID_ACCELEROMETER_INDEX (SYS_numjoysticks - 1)
   12.14 +#define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
   12.15 +
   12.16 +static SDL_Joystick **SYS_Joysticks;
   12.17 +static char **SYS_JoystickNames;
   12.18 +static int SYS_numjoysticks;
   12.19 +static SDL_bool SYS_accelAsJoy;
   12.20 +
   12.21 +/* Function to convert Android keyCodes into SDL ones.
   12.22 + * This code manipulation is done to get a sequential list of codes.
   12.23 + */
   12.24 +int
   12.25 +keycode_to_SDL(int keycode)
   12.26 +{
   12.27 +    int final = 0;
   12.28 +    /* D-Pad key codes (API 1):
   12.29 +     * KEYCODE_DPAD_UP=19, KEYCODE_DPAD_DOWN
   12.30 +     * KEYCODE_DPAD_LEFT, KEYCODE_DPAD_RIGHT, KEYCODE_DPAD_CENTER
   12.31 +     */
   12.32 +    if(keycode < 96)
   12.33 +        return keycode-19;
   12.34 +    /* Some gamepad buttons (API 9):
   12.35 +     * KEYCODE_BUTTON_A=96, KEYCODE_BUTTON_B, KEYCODE_BUTTON_C,
   12.36 +     * KEYCODE_BUTTON_X, KEYCODE_BUTTON_Y, KEYCODE_BUTTON_Z,
   12.37 +     * KEYCODE_BUTTON_L1, KEYCODE_BUTTON_L2,
   12.38 +     * KEYCODE_BUTTON_R1, KEYCODE_BUTTON_R2,
   12.39 +     * KEYCODE_BUTTON_THUMBL, KEYCODE_BUTTON_THUMBR,
   12.40 +     * KEYCODE_BUTTON_START, KEYCODE_BUTTON_SELECT, KEYCODE_BUTTON_MODE
   12.41 +     */
   12.42 +    else if(keycode < 188)
   12.43 +        return keycode-91;
   12.44 +    /* More gamepad buttons (API 12):
   12.45 +     * KEYCODE_BUTTON_1=188 to KEYCODE_BUTTON_16
   12.46 +     */
   12.47 +    else
   12.48 +        return keycode-168;
   12.49 +}
   12.50  
   12.51  /* Function to scan the system for joysticks.
   12.52   * This function should set SDL_numjoysticks to the number of available
   12.53 @@ -43,18 +80,56 @@
   12.54  int
   12.55  SDL_SYS_JoystickInit(void)
   12.56  {
   12.57 -    return (1);
   12.58 +    int i = 0;
   12.59 +    const char *env;
   12.60 +    
   12.61 +    env = SDL_GetHint(SDL_HINT_ACCEL_AS_JOY);
   12.62 +    if (env && !SDL_atoi(env))
   12.63 +        SYS_accelAsJoy = SDL_FALSE;
   12.64 +    else
   12.65 +        SYS_accelAsJoy = SDL_TRUE; /* Default behavior */
   12.66 +    
   12.67 +    SYS_numjoysticks = Android_JNI_GetNumJoysticks();
   12.68 +    if (SYS_accelAsJoy) {
   12.69 +        SYS_numjoysticks++;
   12.70 +    }
   12.71 +    SYS_Joysticks = (SDL_Joystick **)SDL_malloc(SYS_numjoysticks*sizeof(SDL_Joystick *));
   12.72 +    if (SYS_Joysticks == NULL)
   12.73 +    {
   12.74 +        return SDL_OutOfMemory();
   12.75 +    }
   12.76 +    SYS_JoystickNames = (char **)SDL_malloc(SYS_numjoysticks*sizeof(char *));
   12.77 +    if (SYS_JoystickNames == NULL)
   12.78 +    {
   12.79 +        SDL_free(SYS_Joysticks);
   12.80 +        SYS_Joysticks = NULL;
   12.81 +        return SDL_OutOfMemory();
   12.82 +    }
   12.83 +    SDL_memset(SYS_JoystickNames, 0, (SYS_numjoysticks*sizeof(char *)));
   12.84 +    SDL_memset(SYS_Joysticks, 0, (SYS_numjoysticks*sizeof(SDL_Joystick *)));
   12.85 +    
   12.86 +    for (i = 0; i < SYS_numjoysticks; i++)
   12.87 +    {
   12.88 +        if ( SYS_accelAsJoy && i == ANDROID_ACCELEROMETER_INDEX ) {
   12.89 +            SYS_JoystickNames[i] = ANDROID_ACCELEROMETER_NAME;
   12.90 +        } else {
   12.91 +            SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i);
   12.92 +        }
   12.93 +    }
   12.94 +   
   12.95 +    return (SYS_numjoysticks);
   12.96  }
   12.97  
   12.98  int SDL_SYS_NumJoysticks()
   12.99  {
  12.100 -    return 1;
  12.101 +    return SYS_numjoysticks;
  12.102  }
  12.103  
  12.104  void SDL_SYS_JoystickDetect()
  12.105  {
  12.106  }
  12.107  
  12.108 +/* TODO: Hotplugging support */
  12.109  SDL_bool SDL_SYS_JoystickNeedsPolling()
  12.110  {
  12.111      return SDL_FALSE;
  12.112 @@ -64,7 +139,7 @@
  12.113  const char *
  12.114  SDL_SYS_JoystickNameForDeviceIndex(int device_index)
  12.115  {
  12.116 -    return accelerometerName;
  12.117 +    return SYS_JoystickNames[device_index];
  12.118  }
  12.119  
  12.120  /* Function to perform the mapping from device index to the instance id for this index */
  12.121 @@ -81,11 +156,19 @@
  12.122  int
  12.123  SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  12.124  {
  12.125 -    if (device_index == 0) {
  12.126 -        joystick->nbuttons = 0;
  12.127 +    if (device_index < SYS_numjoysticks) {
  12.128          joystick->nhats = 0;
  12.129          joystick->nballs = 0;
  12.130 -        joystick->naxes = 3;
  12.131 +        if (SYS_accelAsJoy && device_index == ANDROID_ACCELEROMETER_INDEX) {
  12.132 +            joystick->nbuttons = 0;
  12.133 +            joystick->naxes = 3;
  12.134 +        } else {
  12.135 +            /* TODO: Get the real number of buttons in the device */
  12.136 +            joystick->nbuttons = 36;
  12.137 +            joystick->naxes = Android_JNI_GetJoystickAxes(device_index);
  12.138 +        }
  12.139 +        
  12.140 +        SYS_Joysticks[device_index] = joystick;
  12.141          return 0;
  12.142      } else {
  12.143          SDL_SetError("No joystick available with that index");
  12.144 @@ -111,7 +194,8 @@
  12.145      Sint16 value;
  12.146      float values[3];
  12.147  
  12.148 -    if (Android_JNI_GetAccelerometerValues(values)) {
  12.149 +    if (SYS_accelAsJoy && Android_JNI_GetAccelerometerValues(values) &&
  12.150 +        joystick->instance_id == ANDROID_ACCELEROMETER_INDEX) {
  12.151          for ( i = 0; i < 3; i++ ) {
  12.152              value = (Sint16)(values[i] * 32767.0f);
  12.153              SDL_PrivateJoystickAxis(joystick, i, value);
  12.154 @@ -129,6 +213,10 @@
  12.155  void
  12.156  SDL_SYS_JoystickQuit(void)
  12.157  {
  12.158 +    SDL_free(SYS_JoystickNames);
  12.159 +    SDL_free(SYS_Joysticks);
  12.160 +    SYS_JoystickNames = NULL;
  12.161 +    SYS_Joysticks = NULL;
  12.162  }
  12.163  
  12.164  SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
  12.165 @@ -151,6 +239,32 @@
  12.166      return guid;
  12.167  }
  12.168  
  12.169 +int
  12.170 +Android_OnPadDown(int padId, int keycode)
  12.171 +{
  12.172 +    SDL_PrivateJoystickButton(SYS_Joysticks[padId], keycode_to_SDL(keycode), SDL_PRESSED);
  12.173 +    
  12.174 +    return 0;
  12.175 +}
  12.176 +
  12.177 +int
  12.178 +Android_OnPadUp(int padId, int keycode)
  12.179 +{
  12.180 +    SDL_PrivateJoystickButton(SYS_Joysticks[padId], keycode_to_SDL(keycode), SDL_RELEASED);
  12.181 +    
  12.182 +    return 0;
  12.183 +}
  12.184 +
  12.185 +int
  12.186 +Android_OnJoy(int joyId, int axis, float value)
  12.187 +{
  12.188 +    /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
  12.189 +    /* TODO: Are the reported values right? */
  12.190 +    SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
  12.191 +    
  12.192 +    return 0;
  12.193 +}
  12.194 +
  12.195  #endif /* SDL_JOYSTICK_ANDROID */
  12.196  
  12.197  /* vi: set ts=4 sw=4 expandtab: */
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/src/joystick/android/SDL_sysjoystick.h	Tue Nov 05 20:07:39 2013 -0300
    13.3 @@ -0,0 +1,28 @@
    13.4 +/*
    13.5 + Simple DirectMedia Layer
    13.6 + Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
    13.7 + 
    13.8 + This software is provided 'as-is', without any express or implied
    13.9 + warranty.  In no event will the authors be held liable for any damages
   13.10 + arising from the use of this software.
   13.11 + 
   13.12 + Permission is granted to anyone to use this software for any purpose,
   13.13 + including commercial applications, and to alter it and redistribute it
   13.14 + freely, subject to the following restrictions:
   13.15 + 
   13.16 + 1. The origin of this software must not be misrepresented; you must not
   13.17 + claim that you wrote the original software. If you use this software
   13.18 + in a product, an acknowledgment in the product documentation would be
   13.19 + appreciated but is not required.
   13.20 + 2. Altered source versions must be plainly marked as such, and must not be
   13.21 + misrepresented as being the original software.
   13.22 + 3. This notice may not be removed or altered from any source distribution.
   13.23 + */
   13.24 +
   13.25 +#include "SDL_config.h"
   13.26 +
   13.27 +extern int Android_OnPadDown(int padId, int keycode);
   13.28 +extern int Android_OnPadUp(int padId, int keycode);
   13.29 +extern int Android_OnJoy(int joyId, int axisnum, float value);
   13.30 +
   13.31 +/* vi: set ts=4 sw=4 expandtab: */