Fixed joystick crash on Android if joystick subsystem not initialized.
authorPhilipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 10 Nov 2013 17:50:40 +0100
changeset 7939 8cff978e06e9
parent 7938 735989a5d280
child 7940 6a69c9795484
Fixed joystick crash on Android if joystick subsystem not initialized.
android-project/src/org/libsdl/app/SDLActivity.java
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Sun Nov 10 17:44:51 2013 +0100
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Sun Nov 10 17:50:40 2013 +0100
@@ -433,6 +433,10 @@
         return mJoystickHandler.getJoystickAxes(joy);
     }
     
+    /**
+     * @param devId the device id to get opened joystick id for.
+     * @return joystick id for device id or -1 if there is none.
+     */
     public static int getJoyId(int devId) {
         return mJoystickHandler.getJoyId(devId);
     }
@@ -606,11 +610,14 @@
         } 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);
+            if (id != -1) {
+                if (event.getAction() == KeyEvent.ACTION_DOWN) {
+                    SDLActivity.onNativePadDown(id, keyCode);
+                } else if (event.getAction() == KeyEvent.ACTION_UP) {
+                    SDLActivity.onNativePadUp(id, keyCode);
+                }
             }
+            return true;
         }
         
         return false;
@@ -826,8 +833,12 @@
         return 0;
     }
     
+    /**
+     * @param devId the device id to get opened joystick id for.
+     * @return joystick id for device id or -1 if there is none.
+     */
     public int getJoyId(int devId) {
-        return 0;
+        return -1;
     }
 }
 
@@ -887,10 +898,12 @@
             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);
+                    if (id != -1) {
+                        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;
             }
         }