[Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 19 Nov 2013 10:00:05 -0300
changeset 8013109299fe73ad
parent 8012 6516c66db5c9
child 8014 0a4b6dec7de5
[Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices

It seems some devices report themselves as DPAD or GAMEPAD and KEYBOARD as well,
and we need to route different keycodes to different parts of SDL.
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
src/joystick/android/SDL_sysjoystick.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Nov 18 20:22:36 2013 -0800
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Tue Nov 19 10:00:05 2013 -0300
     1.3 @@ -245,8 +245,8 @@
     1.4      public static native void nativePause();
     1.5      public static native void nativeResume();
     1.6      public static native void onNativeResize(int x, int y, int format);
     1.7 -    public static native void onNativePadDown(int padId, int keycode);
     1.8 -    public static native void onNativePadUp(int padId, int keycode);
     1.9 +    public static native int onNativePadDown(int padId, int keycode);
    1.10 +    public static native int onNativePadUp(int padId, int keycode);
    1.11      public static native void onNativeJoy(int joyId, int axis,
    1.12                                            float value);
    1.13      public static native void onNativeKeyDown(int keycode);
    1.14 @@ -600,19 +600,22 @@
    1.15      @Override
    1.16      public boolean onKey(View  v, int keyCode, KeyEvent event) {
    1.17          // Dispatch the different events depending on where they come from
    1.18 -        // Some SOURCE_DPAD or SOURCE_GAMEPAD events appear to also be marked as SOURCE_KEYBOARD
    1.19 -        // So, to avoid problems, we process DPAD or GAMEPAD events first.
    1.20 +        // Some SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD
    1.21 +        // So, we try to process them as DPAD or GAMEPAD events first, if that fails we try them as KEYBOARD
    1.22          
    1.23          if ( (event.getSource() & 0x00000401) != 0 || /* API 12: SOURCE_GAMEPAD */
    1.24                     (event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
    1.25              int id = SDLActivity.getJoyId( event.getDeviceId() );
    1.26              if (id != -1) {
    1.27                  if (event.getAction() == KeyEvent.ACTION_DOWN) {
    1.28 -                    SDLActivity.onNativePadDown(id, keyCode);
    1.29 +                    if (SDLActivity.onNativePadDown(id, keyCode) == 0) {
    1.30 +                        return true;
    1.31 +                    }
    1.32                  } else if (event.getAction() == KeyEvent.ACTION_UP) {
    1.33 -                    SDLActivity.onNativePadUp(id, keyCode);
    1.34 +                    if (SDLActivity.onNativePadUp(id, keyCode) == 0) {
    1.35 +                        return true;
    1.36 +                    }
    1.37                  }
    1.38 -                return true;
    1.39              }
    1.40          }
    1.41          
     2.1 --- a/src/core/android/SDL_android.c	Mon Nov 18 20:22:36 2013 -0800
     2.2 +++ b/src/core/android/SDL_android.c	Tue Nov 19 10:00:05 2013 -0300
     2.3 @@ -148,19 +148,19 @@
     2.4  }
     2.5  
     2.6  // Paddown
     2.7 -void Java_org_libsdl_app_SDLActivity_onNativePadDown(
     2.8 +int Java_org_libsdl_app_SDLActivity_onNativePadDown(
     2.9                                      JNIEnv* env, jclass jcls,
    2.10                                      jint padId, jint keycode)
    2.11  {
    2.12 -    Android_OnPadDown(padId, keycode);
    2.13 +    return Android_OnPadDown(padId, keycode);
    2.14  }
    2.15  
    2.16  // Padup
    2.17 -void Java_org_libsdl_app_SDLActivity_onNativePadUp(
    2.18 +int Java_org_libsdl_app_SDLActivity_onNativePadUp(
    2.19                                     JNIEnv* env, jclass jcls,
    2.20                                     jint padId, jint keycode)
    2.21  {
    2.22 -    Android_OnPadUp(padId, keycode);
    2.23 +    return Android_OnPadUp(padId, keycode);
    2.24  }
    2.25  
    2.26  /* Joy */
     3.1 --- a/src/joystick/android/SDL_sysjoystick.c	Mon Nov 18 20:22:36 2013 -0800
     3.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Tue Nov 19 10:00:05 2013 -0300
     3.3 @@ -128,7 +128,6 @@
     3.4              break;
     3.5              
     3.6          default:
     3.7 -            SDL_Log("The button you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL mailing list <sdl@libsdl.org> Android KeyCode %d", keycode);
     3.8              return -1;
     3.9              break;
    3.10      }
    3.11 @@ -313,9 +312,10 @@
    3.12      int button = keycode_to_SDL(keycode);
    3.13      if (button >= 0) {
    3.14          SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
    3.15 +        return 0;
    3.16      }
    3.17      
    3.18 -    return 0;
    3.19 +    return -1;
    3.20  }
    3.21  
    3.22  int
    3.23 @@ -324,9 +324,10 @@
    3.24      int button = keycode_to_SDL(keycode);
    3.25      if (button >= 0) {
    3.26          SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
    3.27 +        return 0;
    3.28      }
    3.29      
    3.30 -    return 0;
    3.31 +    return -1;
    3.32  }
    3.33  
    3.34  int