Fixed joystick crash on Android if joystick subsystem not initialized.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java
index e55b872..60b6aa0 100755
--- a/android-project/src/org/libsdl/app/SDLActivity.java
+++ b/android-project/src/org/libsdl/app/SDLActivity.java
@@ -433,6 +433,10 @@ public class SDLActivity extends Activity {
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 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
} 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 @@ class SDLJoystickHandler {
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 @@ class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
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;
}
}