Fixed bug 3444 - Android-TV: no more handling of back button on remote
authorSam Lantinga <slouken@libsdl.org>
Mon, 17 Oct 2016 22:09:22 -0700
changeset 10551a5bc0d82dca4
parent 10550 d8a72d273dd2
child 10552 221d4601ab8c
Fixed bug 3444 - Android-TV: no more handling of back button on remote

ny00

Unfortunately, simply checking the return codes of "onNativePadDown/Up" as previously done has its own issue:

If an SDL joystick is connected *and* opened, then a proper KeyEvent, say with keycode KEYCODE_BUTTON_1, should lead to an SDL joystick button event as expected.

If, however, the joystick was *not* opened, then "onNativePadDown/Up" will return a negative value, so before the commit from bug 3426, you could unexpectedly get a keyboard event. (In practice, you'll just get a log message, since KEYCODE_BUTTON_1 has no mapping to a proper SDL_ScanCode value, but it's still an problem).

What should still be done, though, is checking the key code itself. We do have the KeyEvent.isGamepadButton method, but according my test, it returns "true" exactly (and only) for the KEYCODE_BUTTON* values, and not for KEYCODE_DPAD* or any other key code.

Here is a possible solution:
- Do check the return codes of "onNativePadDown/Up" as previously done.
- In addition, in "Android_OnPadDown/Up" from src/joystick/android/SDL_sysjoystick.c, 0 should *always* be returned in case the key code can be translated to an SDL_joystick button; Even if no matching joystick can be found.
android-project/src/org/libsdl/app/SDLActivity.java
src/joystick/android/SDL_sysjoystick.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Oct 17 21:47:33 2016 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Oct 17 22:09:22 2016 -0700
     1.3 @@ -1245,14 +1245,15 @@
     1.4          // SOURCE_JOYSTICK, while its key events arrive from the keyboard source
     1.5          // So, retrieve the device itself and check all of its sources
     1.6          if (SDLActivity.isDeviceSDLJoystick(event.getDeviceId())) {
     1.7 -            // Note that we always process a pressed/released button here, as an unopened
     1.8 -            // SDL_Joystick's button press should not be processed as a keyboard's key press
     1.9 +            // Note that we process events with specific key codes here
    1.10              if (event.getAction() == KeyEvent.ACTION_DOWN) {
    1.11 -                SDLActivity.onNativePadDown(event.getDeviceId(), keyCode);
    1.12 -                return true;
    1.13 +                if (SDLActivity.onNativePadDown(event.getDeviceId(), keyCode) == 0) {
    1.14 +                    return true;
    1.15 +                }
    1.16              } else if (event.getAction() == KeyEvent.ACTION_UP) {
    1.17 -                SDLActivity.onNativePadUp(event.getDeviceId(), keyCode);
    1.18 -                return true;
    1.19 +                if (SDLActivity.onNativePadUp(event.getDeviceId(), keyCode) == 0) {
    1.20 +                    return true;
    1.21 +                }
    1.22              }
    1.23          }
    1.24  
     2.1 --- a/src/joystick/android/SDL_sysjoystick.c	Mon Oct 17 21:47:33 2016 -0700
     2.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Mon Oct 17 22:09:22 2016 -0700
     2.3 @@ -187,8 +187,8 @@
     2.4          item = JoystickByDeviceId(device_id);
     2.5          if (item && item->joystick) {
     2.6              SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
     2.7 -            return 0;
     2.8          }
     2.9 +        return 0;
    2.10      }
    2.11      
    2.12      return -1;
    2.13 @@ -203,8 +203,8 @@
    2.14          item = JoystickByDeviceId(device_id);
    2.15          if (item && item->joystick) {
    2.16              SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
    2.17 -            return 0;
    2.18          }
    2.19 +        return 0;
    2.20      }
    2.21      
    2.22      return -1;